<?php

namespace app\api\logic;

use app\admin\model\Form1;
use app\admin\model\Form2;
use app\admin\model\Form3;
use app\admin\model\Form7;
use app\admin\model\Form8;
use app\admin\model\PositiveAlarm;
use app\admin\model\TemperatureAlarm;
use app\BaseController;
use think\Exception;
use think\facade\Cache;
use think\facade\Db;

class FormLogic extends BaseController
{
    public static function createForm1($post)
    {
        Db::startTrans();
        try {
            //验证码校验
            self::checkCode($post);

            if (strtotime($post['arrival_time']) > time()) {
                throw new Exception('到沙时间不能大于当前');
            }
            if (strtotime($post['check_time']) > time()) {
                throw new Exception('最近一次核酸检测时间不能大于当前');
            }
            //阳性报警
            if ($post['check_result'] == 1) {
                (new PositiveAlarm())->save($post);
            }

            //去除短横线
            $post['domicile'] = str_replace('-','',$post['domicile']);
            $post['departure'] = str_replace('-','',$post['departure']);

            //当有未解除的，还重复提交时，将旧的标注为已废弃
            $old = Form1::where([
                'id_card' => $post['id_card'],
                'status'  => 0,
            ])->order('id', 'desc')->limit(1)->find();
            if (!empty($old)) {
                //是否继承发信时间和天数
//                $post['count'] = $old->count;
//                $post['sms_time'] = $old->sms_time;
                $old->status = 2;
                $old->save();
            }

            Form1::create($post);
            Cache::delete('valid_'.$post['mobile']);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::$error = $e->getMessage();
            return false;
        }
    }

    public static function createForm2($post)
    {
        try {
            //验证码校验
            self::checkCode($post);

            if (strtotime($post['vaccination_time']) > time()) {
                throw new Exception('接种时间不能大于当前');
            }
            if (Form2::whereDay('create_time')->where('id_card', $post['id_card'])->count()) {
                throw new Exception('该身份证号今日已经录入了');
            }

            Form2::create($post);
            Cache::delete('valid_'.$post['mobile']);

            return true;
        } catch (\Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }

    public static function createForm3($post)
    {
        try {
            //验证码校验
            self::checkCode($post);

            if (strtotime($post['buy_time']) > time()) {
                throw new Exception('购药时间不能大于当前');
            }
            if (Form3::whereDay('create_time')->where('id_card', $post['id_card'])->count()) {
                throw new Exception('该身份证号今日已经录入了');
            }
//			if ($post['temperature'] > 45 || $post['temperature'] < 30) {
//				throw new Exception('体温只能在30-45之间');
//			}
            //当有未解除的，还重复提交时，将旧的标注为已废弃
//            $old = Form3::where([
//                'id_card' => $post['id_card'],
//                'status'  => 0,
//            ])->order('id', 'desc')->limit(1)->find();
//            if (!empty($old)) {
                //是否继承发信时间和天数
//                $post['count'] = $old->count;
//                $post['sms_time'] = $old->sms_time;
//                $old->status = 2;
//                $old->save();
//            }
//            if (!empty($post['temperature'])) {
//                if (Form7::whereDay('create_time')->where('id_card', $post['id_card'])->count() >= 2 ){
//                    throw new Exception('每天只能提交两次体温');
//                }
//                $time1 = date('Y-m-d 00:00:00', time());
//                $time2 = date('Y-m-d 14:00:00', time());
//                $time3 = date('Y-m-d 23:59:59', time());
//                //查询上午体温是否填报
//                $morning = Form7::where('id_card', $post['id_card'])->whereBetweenTime('create_time', $time1, $time2)->count();
//                //查询下午体温是否填报
//                $afternoon = Form7::where('id_card', $post['id_card'])->whereBetweenTime('create_time', $time2, $time3)->count();
//                if ((time() < strtotime($time2)) && $morning > 0) {
//                    throw new Exception('今日上午已经填报过体温了');
//                }
//                if ((time() < strtotime($time3)) && (time() >= strtotime($time2)) && $afternoon > 0) {
//                    throw new Exception('今日下午已经填报过体温了');
//                }
//                //体温报警记录
//                if ($post['temperature'] > 37.3) {
//                    (new TemperatureAlarm())->save($post);
//                }
//                Form7::create($post);
//            }

            Form3::create($post);
            Cache::delete('valid_'.$post['mobile']);

            return true;
        } catch (\Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }

    public static function createForm7($post)
    {
        try {
            if (!Form1::where('id_card', $post['id_card'])->count()) {
                throw new Exception('请先进行返沙人员信息登记');
            }
			if ($post['temperature'] > 45 || $post['temperature'] < 30) {
				throw new Exception('体温只能在30-45之间');
			}
            //验证码校验
            self::checkCode($post);

            if (Form7::whereDay('create_time')->where('id_card', $post['id_card'])->count() >= 2 ){
                throw new Exception('每天只能提交两次体温');
            }
            $time1 = date('Y-m-d 00:00:00', time());
            $time2 = date('Y-m-d 14:00:00', time());
            $time3 = date('Y-m-d 23:59:59', time());
            //查询上午体温是否填报
            $morning = Form7::where('id_card', $post['id_card'])->whereBetweenTime('create_time', $time1, $time2)->count();
            //查询下午体温是否填报
            $afternoon = Form7::where('id_card', $post['id_card'])->whereBetweenTime('create_time', $time2, $time3)->count();
            if ((time() < strtotime($time2)) && $morning > 0) {
                throw new Exception('今日上午已经填报过体温了');
            }
            if ((time() < strtotime($time3)) && (time() >= strtotime($time2)) && $afternoon > 0) {
                throw new Exception('今日下午已经填报过体温了');
            }
            //体温报警记录
            if ($post['temperature'] > 37.3) {
                (new TemperatureAlarm())->save($post);
            }

            Form7::create($post);
            Cache::delete('valid_'.$post['mobile']);

            return true;
        } catch (\Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }

    public static function createForm8($post)
    {
        try {
            //验证码校验
            self::checkCode($post);

            if (!Form2::where('id_card', $post['id_card'])->count()) {
                throw new Exception('请先进行疫苗接种人员信息登记');
            }

            Form8::create($post);
            Cache::delete('valid_'.$post['mobile']);

            return true;
        } catch (\Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }

    /**
     * 验证码校验
     * @param array $post
     * @throws Exception
     */
    private static function checkCode(array $post)
    {
        //验证码校验
        $code = Cache::get('valid_'.$post['mobile']);
        if (empty($code)) {
            throw new Exception('验证码不存在或已过期');
        }
        if ($post['code'] <> $code) {
            throw new Exception('验证码不正确');
        }
        return true;
    }
}